package com.enthusiasm.engine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

public abstract class MyButton {
	private Bitmap before;
	private Bitmap after;
	private boolean isClick = false;
	private MyRect rect;
	
	public MyButton(Bitmap before, Bitmap after) {
		this.before = before;
		this.after = after;
	}
	
	public void draw(Canvas canvas,Paint paint){
		if(isClick)
			canvas.drawBitmap(after, null, rect, paint);
		else
			canvas.drawBitmap(before, null, rect, paint);
	}
	
	public void setRect(MyRect rect){
		this.rect = rect;
	}
	
	public void onTouchEvent(MotionEvent event){
		int x = (int) event.getX();
		int y = (int) event.getY();
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(rect.contains(x,y)){
				isClick = true;
			}
		}
		else if(event.getAction() == MotionEvent.ACTION_UP){
			if(rect.contains(x,y) && isClick == true){
				isClick = false;
				doClick();
			}
			else{
				isClick = false;
			}
		}
		else if(event.getAction() == MotionEvent.ACTION_MOVE){
			if(!rect.contains(x,y)){
				isClick = false;
			}
		}
	}

	public abstract void doClick();
}
